package com.gowants.pems.util;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import org.springframework.web.multipart.MultipartFile;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;


public class ImageUtil {
	public static void resize1(InputStream inputStream, String fileName, String uploadDir) throws Exception  {
        if (inputStream == null) {
            return;
        }
        BufferedImage src = ImageIO.read(inputStream);
        int old_w = src.getWidth();
        // 得到源图宽
        int old_h = src.getHeight();
        // 得到源图长
        BufferedImage newImg = null;
        // 判断输入图片的类型
        switch (src.getType()) {
        case 13:
          // png,gif
          newImg = new BufferedImage(old_w, old_h, BufferedImage.TYPE_4BYTE_ABGR);
          break;
        default:
          newImg = new BufferedImage(old_w, old_h, BufferedImage.TYPE_INT_RGB);
          break;
        }
        Graphics2D g = newImg.createGraphics();
        // 从原图上取颜色绘制新图
        g.drawImage(src, 0, 0, old_w, old_h, null);
        g.dispose();
        // 根据图片尺寸压缩比得到新图的尺寸
        newImg.getGraphics().drawImage(
        src.getScaledInstance(old_w, old_h, Image.SCALE_SMOOTH), 0,0, null);
        File newFile = new File(uploadDir+fileName);
        String endName = fileName.substring(fileName.lastIndexOf(".")+1).toLowerCase();
        ImageIO.write(newImg, endName, newFile);
  }


    public static String  zipImageFile(MultipartFile mfile, int width, int height,
                                     float quality, String tempPath) throws  Exception {
        if (mfile == null) {
            return null;
        }
        /**对服务器上的临时文件进行处理 */
        InputStream in =mfile.getInputStream();
        Image srcFile = ImageIO.read(in);
        /** 宽,高设定 */
        BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        tag.getGraphics().drawImage(srcFile, 0, 0, width, height, null);
        /** 压缩之后临时存放位置 */
        FileOutputStream out = new FileOutputStream(tempPath);
        JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
        JPEGEncodeParam jep = JPEGCodec.getDefaultJPEGEncodeParam(tag);
        /** 压缩质量 */
        jep.setQuality(quality, true);
        encoder.encode(tag, jep);
        tag.getGraphics().dispose();
        tag.flush();
        out.close();
        srcFile.flush();
        in.close();
        return tempPath;
    }

}
